package com.rocky.project.service;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;



import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.rocky.project.model.base.BaseEntity;
import com.rocky.project.util.Page;
import com.rocky.project.util.PageUtil;
import com.rocky.project.util.Result;

public class BaseDaoImpl extends HibernateDaoSupport implements	BaseDao {
	
	public void clearCache() {
		getHibernateTemplate().clear();
	}
	
	public void create(Object entity) {
		getHibernateTemplate().save(entity);
	}
	
	public void delete(Object entity) {	
		if (entity instanceof BaseEntity) {
			BaseEntity newObj = (BaseEntity) entity;
			newObj.setDeleted(true);
			newObj.setModifyTime(new Date());
			getHibernateTemplate().update(newObj);
			return;
		}
		getHibernateTemplate().update(entity);
	}
	
	public void deleteTrue(Object entity) {
		getHibernateTemplate().delete(entity);
	}	

	public Collection executeHqlNoParam(String queryHql, int startAtIndex,	Integer pageSize) {
		Collection result = null;
		Session session = getHibernateTemplate().getSessionFactory()
				.getCurrentSession();
		Query query = session.createQuery(queryHql.toString());
		query.setFirstResult(startAtIndex);
		if(pageSize!=null){
			query.setMaxResults(pageSize);
		}
		result = query.list();
		return result;
	}

	public Collection executeHqlWithParam(String queryHql, Object[] values,	int startAtIndex, Integer pageSize) {
		Collection result = null;
		Session session = getHibernateTemplate().getSessionFactory()
				.getCurrentSession();
		Query query = session.createQuery(queryHql.toString());
		setQueryParameters(query, values);
		query.setFirstResult(startAtIndex);
		if(pageSize!=null){
			query.setMaxResults(pageSize);	
		}		
		result = query.list();
		return result;
	}

	public void setQueryParameters(Query query, Object args[]) {
		for (int i = 0; i < args.length; i++){			
				query.setParameter(i, args[i]);
		}		
	}

	public Object get(Class cls, Serializable id) {
		return getHibernateTemplate().get(cls, id);
	}

	public int getCountHqlNoParam(String querySql) {
		Collection result = null;
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery(querySql.toString());
		result = query.list();
		return result.size();
	}

	public int getCountHqlWithParam(String querySql, Object[] values) {
		Collection result = null;
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery(querySql.toString());
		setQueryParameters(query, values);
		result = query.list();
		return result.size();
	}

	public Collection loadAll(Class entityClass) {
		return this.getHibernateTemplate().loadAll(entityClass);
	}

	public void update(Object entity) {
		getHibernateTemplate().update(entity);
	}

	public Result executeHqlNoParam(String queryHql, Page page) {
		Result result = new Result();
		Collection resultContent = executeHqlNoParam(queryHql, (page.getEveryPage()*(page.getCurrentPage()-1)), page.getEveryPage());
		int totalRecords = getCountHqlNoParam(queryHql);
		page.setTotalRecords(totalRecords);
		page.setTotalPage(PageUtil.getTotalPage(page.getEveryPage(), totalRecords));
		result.setPage(page);
		if(result!=null){
			result.setContent(resultContent);			
		}	
		return result;
	}

	public Result executeHqlWithParam(String queryHql, Object[] values, Page page) {
		Result result = new Result();
		Collection resultContent = executeHqlWithParam(queryHql, values,(page.getEveryPage()*(page.getCurrentPage()-1)), page.getEveryPage());
		int totalRecords = getCountHqlWithParam(queryHql, values);
		page.setTotalRecords(totalRecords);
		page.setTotalPage(PageUtil.getTotalPage(page.getEveryPage(), totalRecords));
		result.setPage(page);
		if(result!=null){
			result.setContent(resultContent);			
		}	
		return result;
	}

	public void evict(Object entity) {
		getHibernateTemplate().evict(entity);
		
	}

	public void merge(Object entity) {
		getHibernateTemplate().merge(entity);		
	}


	
}
